﻿#region Copyright & License
//
// Copyright 2010 sd&c Hofmann 
// 
// mailto:info@sdch.de
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SDCH.SharePoint.Log4Sandbox.UserCode.ProxyOperationArgs;
using Microsoft.SharePoint.Utilities;
using System.Reflection;

namespace SDCH.SharePoint.Log4Sandbox.UserCode
{
    [Serializable]
    public class LogContext
    {
        
        
        public string Logger { get; set; }
        public string Repository { get; set; }
        public bool IsDebugEnabled { get; set; }
        public bool IsInfoEnabled { get; set; }
        public bool IsWarnEnabled { get; set; }
        public bool IsErrorEnabled { get; set; }
        public bool IsFatalEnabled { get; set; }


        public void Debug(string pMessage) {
            if (IsDebugEnabled)
            {
                LogArgs log = CreateLogArgs(MethodInfo.GetCurrentMethod(), pMessage);
                ExecuteLog(log);
            }
        }

        public void Debug(string pMessage, Exception pException) {
            if (IsDebugEnabled) {
                LogArgs log = CreateLogArgs(MethodInfo.GetCurrentMethod(), pMessage, pException);
                ExecuteLog(log);
            }
        }

        public void Warn(string pMessage)
        {
            if (IsWarnEnabled)
            {
                LogArgs log = CreateLogArgs(MethodInfo.GetCurrentMethod(), pMessage);
                ExecuteLog(log);
            }
        }

        public void Warn(string pMessage, Exception pException)
        {
            if (IsWarnEnabled)
            {
                LogArgs log = CreateLogArgs(MethodInfo.GetCurrentMethod(), pMessage, pException);
                ExecuteLog(log);
            }
        }

        public void Info(string pMessage)
        {
            if (IsInfoEnabled)
            {
                LogArgs log = CreateLogArgs(MethodInfo.GetCurrentMethod(), pMessage);
                ExecuteLog(log);
            }
        }

        public void Info(string pMessage, Exception pException)
        {
            if (IsInfoEnabled)
            {
                LogArgs log = CreateLogArgs(MethodInfo.GetCurrentMethod(), pMessage, pException);
                ExecuteLog(log);
            }
        }

        public void Fatal(string pMessage)
        {
            if (IsFatalEnabled)
            {
                LogArgs log = CreateLogArgs(MethodInfo.GetCurrentMethod(), pMessage);
                ExecuteLog(log);
            }
        }

        public void Fatal(string pMessage, Exception pException)
        {
            if (IsFatalEnabled)
            {
                LogArgs log = CreateLogArgs(MethodInfo.GetCurrentMethod(), pMessage, pException);
                ExecuteLog(log);
            }
        }

        public void Error(string pMessage)
        {
            if (IsErrorEnabled)
            {
                LogArgs log = CreateLogArgs(MethodInfo.GetCurrentMethod(), pMessage);
                ExecuteLog(log);
            }
        }

        public void Error(string pMessage, Exception pException)
        {
            if (IsErrorEnabled)
            {
                LogArgs log = CreateLogArgs(MethodInfo.GetCurrentMethod(), pMessage, pException);
                ExecuteLog(log);
            }
        }

        private LogArgs CreateLogArgs(MethodBase pMethodBase, params object[] pMethodParameterValues)
        {
            LogArgs args = new LogArgs(this);
            args.Method = pMethodBase.Name;
            args.Parameters = pMethodParameterValues;
            args.ParameterTypes = (from par in pMethodBase.GetParameters()
                                   select par.ParameterType).ToArray();
            return args;

        }

        private void ExecuteLog(LogArgs log)
        {
            SPUtility.ExecuteRegisteredProxyOperation(LogContextManager.Log4SandboxAssemblyName, LogContextManager.Log4SandboxLogOperationTypeName, log);
        }
    }
}
